package com.selene.sqlSession;

import com.selene.pojo.Configuration;
import com.selene.pojo.MappedStatement;

import java.lang.reflect.*;
import java.util.List;

public class DefaultSqlSession implements SqlSession {
    private Configuration configuration;

    public DefaultSqlSession(Configuration configuration) {
        this.configuration = configuration;
    }

    @Override
    public void insertOne(String statementId, Object... params) throws Exception {
        updateOne(statementId, params);
    }

    @Override
    public void updateOne(String statementId, Object... params) throws Exception {
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        simpleExecutor.query(configuration, mappedStatement, params);
    }

    @Override
    public void deleteOne(String statementId, Object... params) throws Exception {
        updateOne(statementId, params);
    }

    @Override
    public <E> List<E> selectList(String statementId, Object... params) throws Exception {
        SimpleExecutor simpleExecutor = new SimpleExecutor();
        MappedStatement mappedStatement = configuration.getMappedStatementMap().get(statementId);
        List<Object> resultList = simpleExecutor.query(configuration, mappedStatement, params);
        return (List<E>) resultList;
    }

    @Override
    public <T> T selectOne(String statementId, Object... params) throws Exception {
        List<Object> objects = selectList(statementId, params);
        if (objects.size() == 1) {
            return (T) objects.get(0);
        } else {
            throw new RuntimeException("Error results.");
        }
    }

    @Override
    public <T> T getMapper(Class<?> mapperClass) {
        //使用JDK动态代理来为Dao接口生成代理对象
        Object proxyInstance = Proxy.newProxyInstance(DefaultSqlSession.class.getClassLoader(), new Class[]{mapperClass}, new InvocationHandler() {
            @Override
            public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
                //本质仍然是实现jdbc //根据不同情况决定是执行selectOne还是selectList
                //准备参数1.statementId: namespace.id = 接口全限定名.方法名(通过Method可以拿到接口全限定名和方法名)
                String methodName = method.getName();
                String className = method.getDeclaringClass().getName();
                String statementId = className + "." + methodName;
                //准备参数2：params：实际就是args
                //根据调用方法的返回结果判断是执行selectOne还是selectList
//                Type genericReturnType = method.getGenericReturnType();
//                //判断是否进行了范型类型参数话（当前返回值类型是否由范型）
//                if (genericReturnType instanceof ParameterizedType){
//                    List<Object> objects = selectList(statementId, args);
//                    return objects;
//                }else {
//                    return selectOne(statementId, args);
//                }
                if (methodName.equals("selectList")) {
                    return selectList(statementId, args);
                } else if (methodName.equals("selectOne")) {
                    return selectOne(statementId, args);
                } else if (methodName.equals("updateOne")) {
                    updateOne(statementId, args);
                    return null;
                } else if (methodName.equals("deleteOne")) {
                    deleteOne(statementId, args);
                    return null;
                } else if (methodName.equals("insertOne")) {
                    insertOne(statementId, args);
                    return null;
                } else {
                    throw new RuntimeException("Error method name");
                }
            }
        });
        return (T) proxyInstance;
    }
}
